package com.admin.web.controller;


import com.admin.web.annotation.LogMenthodName;
import com.admin.web.controller.commen.BaseController;
import com.alibaba.fastjson.JSON;
import com.fegin.bean.admin.AdminUser;
import com.fegin.bean.common.layuiBean.ResultPage;
import com.fegin.bean.common.layuiBean.VoPage;
import com.fegin.client.admin.UserFegin;
import com.util.Respons.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-01-08
 */
@Controller
@RequestMapping("/admin/user")
public class UserController extends BaseController {
    @Autowired
    UserFegin userFegin;

    /**
     * 登陆页面
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/login.html", method = RequestMethod.GET)
    public Object login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //判断如果是ajax请求 来到了这里 说明session过期 或者是 当前登陆的用户已失效
        if (isAjaxRequest(request)) {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(JSON.toJSONString(ResponseUtil.getLoginAgainResponseMap()));//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
            return null;
        } else {
            return "login";
        }
    }

    /**
     * 首页页面
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("user", getUser());
        return "index";
    }

    /**
     * 用户管理页面
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/main.html", method = RequestMethod.GET)
    public String main() throws IOException {
        return "system/user";
    }

    /**
     * 添加用户页面
     *
     * @return
     */
    @RequiresPermissions("button:用户管理添加")
    @RequestMapping(value = "/userAdd.html", method = RequestMethod.GET)
    public String userAdd() {
        return "system/user_add";
    }

    /**
     * 分销商添加页面
     *
     * @return
     */
    @RequestMapping(value = "/peopleAdd.html", method = RequestMethod.GET)
    public String peopleAdd() {
        return "distribution/people_add";
    }


    /**
     * 表格分页查询
     *
     * @param voPage
     * @return
     */
    @RequestMapping(value = "/findUser", method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<AdminUser> findUser(VoPage voPage ) {
        return userFegin.findUser(voPage,getUser().getId());
    }


    @LogMenthodName(name = "AOP环绕用户添加")
    @RequestMapping(value = "/insertUser", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("button:用户管理添加")
    public Map<String, Object> insertUser(AdminUser user, Long roleId) {
        return userFegin.insertUser(user, roleId,getUser().getId());
    }

    @LogMenthodName(name = "AOP环绕用户删除")
    @RequestMapping(value = "/delUser", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("button:用户管理删除")
    public Map<String, Object> delUser(Long userId, String userName) {
        return userFegin.delUser(userId, userName);
    }


    /**
     * 登陆请求
     *
     * @param userName
     * @param passWord
     * @return
     */
    @LogMenthodName(name = "AOP环绕登陆请求")
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> doLogin(String userName, String passWord) {
        //添加用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord);
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(token);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.getNoUser();
        }
        return ResponseUtil.getSuccessMap();
    }




    /**
     * 分销人员页面
     *
     * @return
     */

    @RequestMapping(value = "/distributionPeoPle.html", method = RequestMethod.GET)
    public String addDistributionPeoPle() {
        return "distribution/people";
    }


    /**
     * 分销人员表格查询
     *
     * @return
     */

    @RequestMapping(value = "/findDistributionPeoPleByPage", method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<AdminUser> findDistributionPeoPleByPage(VoPage voPage) {
        ResultPage<AdminUser> li=userFegin.findDistributionPeoPleByPage(voPage);
        return li;
    }



}
